﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using intelliEssay.Core;
using intelliEssay.Core.DocumentStructure;
using intelliEssay.Interop;
using System.Collections;

namespace intelliEssay.Checkers.DirectoryChecker
{
    public class DirectoryChecker : IEssayChecker
    {
        public void Run(Task task)
        {
            //throw new NotImplementedException();
            CheckDirectoy(task);
        }

        public void Cancel(Task task)
        {
            throw new NotImplementedException();
        }

        public string GetDescription(string cultureInfo)
        {
            //throw new NotImplementedException();
            return "Checks directory";
        }

        public string GetName(string cultureInfo)
        {
            //throw new NotImplementedException();
            return "Directory Checker";
        }
        public void CommonRunBug(Run run,RunInfoEx standardRuninfo, Task task)
        {
           // RunInfoEx standardRuninfo = new RunInfoEx(task.taskSetting.standardDocument, run, HostApplications.MsOffice2010);
            RunInfoEx targetRuninfo = new RunInfoEx(task.TaskObjectRoot, run, HostApplications.MsOffice2010);
            if (targetRuninfo["isbold"] != standardRuninfo["isbold"])
            {
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处是否加粗出现错误", run, task, true));
            }
            //if (targetRuninfo["isunderline"] != standardRuninfo["isunderline"])
            //{
            //    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处下划线错误", run, task, true));
            //}
            if (targetRuninfo["isitalic"] != standardRuninfo["isitalic"])
            {
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处是否倾斜有错误", run, task, true));
            }
            if (standardRuninfo["color"] != targetRuninfo["color"] && targetRuninfo["color"] != "auto" && targetRuninfo["color"] != "0000FF")
            {
       
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的颜色不正确", run, task, true));
            }
        }

        public void CheckDirectoy(Task task)
        {
            //var contents = task.documentStructure.MenuList;
            //for (int i = 0;i< contents.Count; i++)
            //{
            //    var currentPargraphRuns = contents[i].Elements<Run>().ToArray();
                
            //    RunInfoEx standardRunInfo = new RunInfoEx(task.taskSetting.standardDocument,task
            //    for (int j = 0; j < currentPargraphRuns.Count(); j++)
            //    {
            //        RunInfoEx currentRunInfo = new RunInfoEx(task.TaskObjectRoot, currentPargraphRuns[j], HostApplications.MsOffice2010);
            //        currentRunInfo["fontsize"];
            //    }
            //}
            //Pa

           // task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font,"",
            Paragraph Directory = task.documentStructure.Menu;
            if (Directory == null)
            {
                return;
            }
            List<Paragraph> DirectoryContent = task.documentStructure.MenuList;
            //先检查目录两个字的格式

            //检查目录这两个字是否正确
            //这样返回的是一个标准的目录的那个一段
            Paragraph standdirectory = Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(DocumentParts.Menu, 0, task);
            Paragraph standdirectorycontent = Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(DocumentParts.MenuContent, 0, task);
            RunInfoEx standarddirectoryRuninfo = new RunInfoEx(task.taskSetting.standardDocument, standdirectory.Elements<Run>().First(), HostApplications.MsOffice2010);
            RunInfoEx standarddirectorycontentRuninfo = new RunInfoEx(task.taskSetting.standardDocument,standdirectorycontent.Elements<Run>().First(),HostApplications.MsOffice2010);
#region
            if (Directory.InnerText != standdirectory.InnerText)
            {
                task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Element, "目录中间缺少空格或多空格", Directory, task, true));
            }
            foreach (Run run in Directory.Elements<Run>())
            {
                if (run.InnerText.Replace(" ", "") != "")
                {
                    if (run.InnerText.ToString().First() >= 'A' && run.InnerText.ToString().First() <= 'z')
                    { continue; }
                    else
                    {
                        CommonRunBug(run, standarddirectoryRuninfo, task);

                        RunInfoEx targetRuninfo = new RunInfoEx(task.TaskObjectRoot, run, HostApplications.MsOffice2010);
                        if (standarddirectoryRuninfo["runfonts"] != targetRuninfo["runfonts"])
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的格式不正确", run, task, true));
                        }
                        if (standarddirectoryRuninfo["fontsize"] != targetRuninfo["fontsize"])
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处字体的大小不正确", run, task, true));
                        }
                    }
                }
                else
                { continue; }
            }


            #endregion
            //检查目录的内容的格式
            #region
            foreach (Paragraph paragraph in DirectoryContent)
            {
                List<Hyperlink> hyperlink = new List<Hyperlink>();
                hyperlink = paragraph.Elements<Hyperlink>().ToList();
                if (hyperlink.Count != 0)
                {
                    if (paragraph.Elements<Run>().ToList().Count != 0)
                    {
                        foreach (Run run1 in paragraph.Elements<Run>())
                        {
                            if (run1.InnerText.Trim() != "" && !run1.InnerText.StartsWith(" PAGEREF") && !run1.InnerText.StartsWith(" HYPERLINK") && !run1.InnerText.Replace(" ", "").StartsWith("TOC"))
                            {
                                if (run1.InnerText.ToString().First() >= 'A' && run1.InnerText.ToString().First() <= 'z')
                                { continue; }
                                else
                                {
                                    CommonRunBug(run1, standarddirectorycontentRuninfo, task);
                                    RunInfoEx targetRuninfo = new RunInfoEx(task.TaskObjectRoot, run1, HostApplications.MsOffice2010);
                                    if (targetRuninfo["runfonts"] != standarddirectorycontentRuninfo["runfonts"] && targetRuninfo["runfonts"] != "Times New Roman")
                                    {
                                        task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处 的字体不正确", run1, task, true));
                                    }
                                }
                            }
                            else
                            { continue; }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < hyperlink.Count(); i++)
                        {
                            foreach (Run run1 in hyperlink[i].Elements<Run>())
                            {
                               
                                if (run1.InnerText.Trim() != "" && !run1.InnerText.StartsWith(" PAGEREF") && !run1.InnerText.StartsWith(" HYPERLINK") && !run1.InnerText.Replace(" ", "").StartsWith("TOC"))
                            {
                                if (run1.InnerText.ToString().First() >= 'A' && run1.InnerText.ToString().First() <= 'z')
                                { continue; }
                                else
                                {
                                    CommonRunBug(run1, standarddirectorycontentRuninfo, task);
                                    RunInfoEx targetRuninfo = new RunInfoEx(task.TaskObjectRoot, run1, HostApplications.MsOffice2010);
                                    if (targetRuninfo["runfonts"] != standarddirectorycontentRuninfo["runfonts"] && targetRuninfo["runfonts"] != "Times New Roman" && targetRuninfo["runfonts"] != "黑体")
                                    {
                                        // Console.WriteLine(int.Parse(run1.InnerText.Replace(".", "")));
                                        // Console.WriteLine(run1.InnerText);
                                        //  Console.WriteLine(targetRuninfo["runfonts"]);
                                        if (run1.InnerText.ToString()[0] < 128)
                                        { continue; }
                                        else
                                        {
                                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处 的字体不正确", run1, task, true));
                                        }
                                    }
                                }
                                
                            }
                            else
                            { continue; }

                            }
                        }
                    }
                }
                else
                {
                    foreach(Run run1 in paragraph.Elements<Run>())
                    {
                        if (run1.InnerText.Trim() != "" && !run1.InnerText.StartsWith(" PAGEREF") && !run1.InnerText.StartsWith(" HYPERLINK") && !run1.InnerText.Replace(" ", "").StartsWith("TOC"))
                        {
                            if (run1.InnerText.ToString().First() >= 'A' && run1.InnerText.ToString().First() <= 'z')
                            { continue; }
                            else
                            {
                                CommonRunBug(run1, standarddirectorycontentRuninfo, task);
                                RunInfoEx targetRuninfo = new RunInfoEx(task.TaskObjectRoot, run1, HostApplications.MsOffice2010);
                                if (targetRuninfo["runfonts"] != standarddirectorycontentRuninfo["runfonts"] && targetRuninfo["runfonts"] != "Times New Roman")
                                {
                                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Font, "此处 的字体不正确", run1, task, true));
                                }
                            }
                        }
                        else
                        { continue; }
                    }
                }
            }
            #endregion


        }
    }
}
